﻿using Infrastructure.Config;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Text;
using Utility;

namespace OtherLib.TenXun
{
    public static class WxMiniUtil
    {
        private static string APP_ID = "wx2e31df66a3e7f974"; //appid
        private static string APP_SECRET = "d78c719fc4fb93d46d73680bf668c2b5"; //appsecret


        static WxMiniUtil()
        {
            if (string.IsNullOrWhiteSpace(APP_ID))
            {
                var aliSmsConfig = ApplicationConfig.GetConfig().GetSection("TenXunSecret");

                APP_ID = aliSmsConfig["AccessKeyId"];
                APP_SECRET = aliSmsConfig["Secret"];

            }
        }

        /// <summary>
        /// 用code获取opendid
        /// </summary>
        /// <param name="appid"></param>
        /// <param name="appsecret"></param>
        /// <param name="code"></param>
        /// <returns></returns>
        public static JObject GetOpendidAndSessionkey( string code)
        {
            string url = "https://api.weixin.qq.com/sns/jscode2session?appid=" + APP_ID + "&secret=" + APP_SECRET + "&js_code=" + code + "&grant_type=authorization_code";

            JObject JO = new JObject();
            var str=HttpUtil.GetText(url).GetAwaiter().GetResult();

            JO = (JObject)JsonConvert.DeserializeObject(str);

            return JO;
        }
    }
}
